import { _decorator, Component, Node, Rect, Size, UITransform, Vec3, view } from 'cc';
import { globalObserver } from '../Observer/GlobalObserver';
const { ccclass, property } = _decorator;

@ccclass('PartActive')
export class PartActive extends Component {
    private playerLocation: Vec3 = new Vec3();
    private startPosition: Vec3 = new Vec3();
    private uitSize: Size = new Size();
    private area: Size = new Size();

    protected onLoad(): void {
        this.area = view.getVisibleSize();
        this.node.active = false;
        this.startPosition = this.node.getPosition();
        this.uitSize = this.node.getComponent(UITransform).contentSize;
        globalObserver.on('update-position', this.getLocation, this);
    }

    getLocation(pos: Vec3){
        this.playerLocation = pos;

        // camera area
        const viewRect = new Rect(
            this.playerLocation.x - this.area.width / 2,
            this.playerLocation.y - this.area.height / 2,
            this.area.width+128,
            this.area.height+128
        );

        // this node area
        const blockRect = new Rect(
            this.startPosition.x,
            this.startPosition.y,
            this.uitSize.width,
            this.uitSize.height
        );

        if (viewRect.intersects(blockRect)) {
            this.node.active = true;
        } else {
            this.node.active = false;
        }
    }
        
//        // only load slices that player on
//        if (this.playerLocation.x >= this.converArea[0] && this.playerLocation.x <= this.converArea[2] && this.playerLocation.y >= this.converArea[1] && this.playerLocation.y <= this.converArea[3]) {
//            this.node.active = true;
//        } else {
//            this.node.active = false;
//        }
//    }

    update(deltaTime: number) {
        
    }
}


